package com.nsu.konus.control.ui.preferences;

import java.io.File;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.nsu.konus.control.Activator;
import com.nsu.konus.control.preferences.KonusPreference;

public class KonusPreferencePage extends FieldEditorPreferencePage implements
	IWorkbenchPreferencePage  {
	private KonusPreference prefc = null;
	private FileFieldEditor dosboxFieldEditor = null;
	private DirectoryFieldEditor konusFieldEditor = null;
	private DirectoryFieldEditor russifierFieldEditor = null;
	private DirectoryFieldEditor fortranFieldEditor = null;
	private StringFieldEditor solvingTimeFieldEditor = null;

	public KonusPreferencePage() {
		super(GRID);
		prefc = new KonusPreference(Activator.getDefault().getPluginPreferences());
		setDescription("Konus preference page");
	}

	// @Override
	public void createFieldEditors() {
		//addField(createToDefaultEditor());
		createAll();
		addField(dosboxFieldEditor);
		addField(konusFieldEditor);
		addField(russifierFieldEditor);
		addField(fortranFieldEditor);
		addField(solvingTimeFieldEditor);
	}
	
	protected void performDefaults() {
		//super.performDefaults();
		KonusPreference dpref = KonusPreference.getDefault();
		dosboxFieldEditor.setStringValue(dpref.getDosboxPath());
		konusFieldEditor.setStringValue(dpref.getKonusPath());
		russifierFieldEditor.setStringValue(dpref.getRussifierPath());
		fortranFieldEditor.setStringValue(dpref.getFortranPath());
		solvingTimeFieldEditor.setStringValue(dpref.getSolverWorkingTime() + "");
	}
	
	private void createAll() {
		createDosboxFieldEditor();
		createKonusFieldEditor();
		createRussifierFieldEditor();
		createFortranFieldEditor();
		createSolvingTimeFieldEditor();
	}
	
	/*
	private FieldEditor createToDefaultEditor() {
		BooleanFieldEditor editor = new BooleanFieldEditor(
				KonusPreference.USE_DEFAULT, "Use default", getFieldEditorParent()) {
			// @Override
			public void store() {
				super.store();
				prefc.setUseDefault(this.getBooleanValue());
			}
		};
		editor.(prefc.isUseDefault(), getFieldEditorParent());
		return editor;
	}
	*/
	private void createDosboxFieldEditor() {
		dosboxFieldEditor = new FileFieldEditor(KonusPreference.DOSBOX_PATH,
				"DosBox Path", getFieldEditorParent()) {
			public boolean doCheckState() {
				return super.doCheckState()
						&& new File(getStringValue()).getName().equals("dosbox.exe");
			}

			// @Override
			public void store() {
				super.store();
				prefc.setDosboxPath(this.getStringValue());
			}
		};
		//result.setEnabled(!prefc.isUseDefault(), getFieldEditorParent());
		dosboxFieldEditor.setStringValue(prefc.getDosboxPath());
		dosboxFieldEditor.setErrorMessage("This file should be named \"dosbox.exe\"");
	}
	
	private void createKonusFieldEditor() {
		konusFieldEditor = new DirectoryFieldEditor(KonusPreference.KONUS_PATH,
				"Konus Path", getFieldEditorParent()) {
			public boolean doCheckState() {
				return super.doCheckState()
						&& new Path(getStringValue() + "/dataedit.exe").toFile().exists();
			}

			// @Override
			public void store() {
				super.store();
				prefc.setKonusPath(this.getStringValue());
			}
		};
		//result.setEnabled(!prefc.isUseDefault(), getFieldEditorParent());
		konusFieldEditor.setStringValue(prefc.getKonusPath());
		konusFieldEditor.setErrorMessage("This directory should contain \"dataedit.exe\"");
	}

	private void createRussifierFieldEditor() {
		russifierFieldEditor = new DirectoryFieldEditor(KonusPreference.RUSSIFIER_PATH,
				"Path to russifier", getFieldEditorParent()) {
			public boolean doCheckState() {
				return super.doCheckState()
						&& new Path(getStringValue() + "/RK.COM").toFile().exists();
			}

			// @Override
			public void store() {
				super.store();
				prefc.setRussifierPath(this.getStringValue());
			}
		};
		//result.setEnabled(!prefc.isUseDefault(), getFieldEditorParent());
		russifierFieldEditor.setStringValue(prefc.getRussifierPath());
		russifierFieldEditor.setErrorMessage("This directory should contain \"RK.COM\"");
	}

	private void createFortranFieldEditor() {
		fortranFieldEditor = new DirectoryFieldEditor(KonusPreference.FORTRAN_PATH,
				"Fortran Path", getFieldEditorParent()) {
			public boolean doCheckState() {
				return super.doCheckState()
						&& new Path(getStringValue() + "/BINB").toFile().exists();
			}

			// @Override
			public void store() {
				super.store();
				prefc.setFortranPath(this.getStringValue());
			}
		};
		//result.setEnabled(!prefc.isUseDefault(), getFieldEditorParent());
		fortranFieldEditor.setStringValue(prefc.getFortranPath());
		fortranFieldEditor.setErrorMessage("This directory should contain \"BINB\" directory");
	}
	
	private void createSolvingTimeFieldEditor() {
		solvingTimeFieldEditor = new StringFieldEditor(KonusPreference.SOLVER_WORKING_TIME,
				"Solver Working Time", getFieldEditorParent()) {
			public boolean doCheckState() {
				if (!super.doCheckState())
				{
					setErrorMessage("Enter the solver working time");
					return false;
				}
				try {
					Integer.parseInt(getStringValue());
				}
				catch(Exception e) {
					setErrorMessage("Value should be an integer");
					return false;
				}
				return true;
			}

			// @Override
			public void store() {
				super.store();
				prefc.setSolverWorkingTime(Integer.parseInt(this.getStringValue()));
			}
		};
		//editor.setEnabled(!prefc.isUseDefault(), getFieldEditorParent());
		solvingTimeFieldEditor.setStringValue(prefc.getSolverWorkingTime() + "");
	}

	public void init(IWorkbench workbench) {
	}

	// @Override
	public boolean performOk() {
		super.performOk();
		Activator.getDefault().savePluginPreferences();
		return true;
	}
	
}
